package me.dqn.noway.window;

import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.Toast;

import me.dqn.noway.R;
import me.dqn.noway.bean.NoWayContext;
import me.dqn.noway.bean.PasswordInformation;

public class AddPopWindow extends PopupWindow {
    View contentView;

    public AddPopWindow(View contentView) {
        super(contentView, RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        this.contentView = contentView;
        setFocusable(true);
        setOutsideTouchable(true);
        setAnimationStyle(R.style.PopupWindow);
        showAtLocation(contentView, Gravity.BOTTOM, 0, 0);
        setUpComponents(contentView);
    }

    private void setUpComponents(View contentView) {
        EditText usernameEdit = contentView.findViewById(R.id.add_name);
        EditText passwordEdit = contentView.findViewById(R.id.add_password);
        EditText mailEdit = contentView.findViewById(R.id.add_mail);
        EditText detailEdit = contentView.findViewById(R.id.add_detail);
        EditText typeEdit = contentView.findViewById(R.id.add_type);
        Button postBtn = contentView.findViewById(R.id.add_post);
        Button clearBtn = contentView.findViewById(R.id.add_clear);
        postBtn.setOnClickListener(listener -> {
            // 添加数据
            PasswordInformation passwordInformation = new PasswordInformation();
            passwordInformation.setType(typeEdit.getText().toString());
            passwordInformation.setUsername(usernameEdit.getText().toString());
            passwordInformation.setPassword(passwordEdit.getText().toString());
            passwordInformation.setMail(mailEdit.getText().toString());
            passwordInformation.setDetail(detailEdit.getText().toString());

            // 上传
            if ("OK".equals(passwordInformation.validate())) {
                NoWayContext.getINSTANCE().getMyPresenter().addInfo(passwordInformation);
            } else {
                NoWayContext.getINSTANCE().getMyPresenter().toastMessages(passwordInformation.validate(), Toast.LENGTH_SHORT);
            }
            AddPopWindow.this.dismiss();
        });

        clearBtn.setOnClickListener(listener -> {
            usernameEdit.setText("");
            passwordEdit.setText("");
            mailEdit.setText("");
            detailEdit.setText("");
            typeEdit.setText("");
        });
    }
}
